package com.eshop.message.redis.listener;

import org.slf4j.LoggerFactory;
import java.lang.invoke.SerializedLambda;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.modules.activity.domain.StorePink;
import com.eshop.enums.OrderInfoEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.modules.order.domain.StoreOrder;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.redis.connection.Message;
import com.eshop.modules.activity.service.StorePinkService;
import com.eshop.modules.order.service.StoreOrderService;
import com.eshop.message.redis.config.RedisConfigProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.data.redis.connection.MessageListener;

@Component
public class RedisKeyExpirationListener implements MessageListener
{
    private static final Logger log;
    private RedisTemplate<String, String> redisTemplate;
    private RedisConfigProperties redisConfigProperties;
    private StoreOrderService storeOrderService;
    private StorePinkService storePinkService;
    
    public RedisKeyExpirationListener(final RedisTemplate<String, String> redisTemplate, final RedisConfigProperties redisConfigProperties, final StoreOrderService storeOrderService, final StorePinkService storePinkService) {
        this.redisTemplate = redisTemplate;
        this.redisConfigProperties = redisConfigProperties;
        this.storeOrderService = storeOrderService;
        this.storePinkService = storePinkService;
    }
    
    public void onMessage(final Message message, final byte[] bytes) {
        final RedisSerializer<?> serializer = (RedisSerializer<?>)this.redisTemplate.getValueSerializer();
        final String channel = String.valueOf(serializer.deserialize(message.getChannel()));
        String body = String.valueOf(serializer.deserialize(message.getBody()));
        if (StrUtil.format((CharSequence)"__keyevent@{}__:expired", new Object[] { this.redisConfigProperties.getDatabase() }).equals(channel)) {
            if (body.contains("order:unpay:")) {
                body = body.replace("order:unpay:", "");
                RedisKeyExpirationListener.log.info("body:{}", body);
                final String orderId = body;
                final StoreOrder order = (StoreOrder)this.storeOrderService.getOne((Wrappers.<StoreOrder>lambdaQuery().eq(StoreOrder::getId, orderId)).eq(StoreOrder::getPaid, OrderInfoEnum.PAY_STATUS_0.getValue()));
                if (order != null) {
                    this.storeOrderService.cancelOrder(order.getOrderId(), (Long)null);
                    RedisKeyExpirationListener.log.info("订单id:{},未在规定时间支付取消成功", body);
                }
            }
            if (body.contains("order:unconfirm:")) {
                body = body.replace("order:unconfirm:", "");
                RedisKeyExpirationListener.log.info("body:{}", body);
                final String orderId = body;
                final StoreOrder order = (StoreOrder)this.storeOrderService.getOne(((Wrappers.<StoreOrder>lambdaQuery().eq(StoreOrder::getId, orderId)).eq(StoreOrder::getPaid, OrderInfoEnum.PAY_STATUS_1.getValue())).eq(StoreOrder::getStatus, OrderInfoEnum.STATUS_1.getValue()));
                if (order != null) {
                    this.storeOrderService.takeOrder(order.getOrderId(), (Long)null);
                    RedisKeyExpirationListener.log.info("订单id:{},自动收货成功", body);
                }
            }
            if (body.contains("pink:cancel:")) {
                body = body.replace("pink:cancel:", "");
                RedisKeyExpirationListener.log.info("body:{}", body);
                final String pinkId = body;
                final StorePink storePink = (StorePink)this.storePinkService.getOne(((Wrappers.<StorePink>lambdaQuery().eq(StorePink::getId, pinkId)).eq(StorePink::getStatus, OrderInfoEnum.PINK_STATUS_1.getValue())).eq(StorePink::getIsRefund, OrderInfoEnum.PINK_REFUND_STATUS_0.getValue()));
                if (storePink != null) {
                    this.storePinkService.removePink(storePink.getUid(), storePink.getCid(), storePink.getId());
                    RedisKeyExpirationListener.log.info("拼团订单id:{},未在规定时间完成取消成功", body);
                }
            }
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RedisKeyExpirationListener.class);
    }
}
